﻿using ezCloud.Api.Models;
using ezCloud.Data;
using Newtonsoft.Json;
using Newtonsoft.Json.Serialization;
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Runtime.Serialization;
using System.Web;
using System.Web.Http;
using System.Web.Mvc;
using System.Web.Optimization;
using System.Web.Routing;
using Quartz;
using Quartz.Impl;
using Quartz.Job;
using System.Threading.Tasks;

namespace ezCloud.Api
{
   

    public class WebApiApplication : System.Web.HttpApplication
    {
        public static bool flagRunProcessAutoAssignRoom = true;
        protected void Application_Start()
        {
            AreaRegistration.RegisterAllAreas();
            GlobalConfiguration.Configure(WebApiConfig.Register);
            FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
            RouteConfig.RegisterRoutes(RouteTable.Routes);
            BundleConfig.RegisterBundles(BundleTable.Bundles);
            //Configure Audit Scheduler


            /*GlobalConfiguration.Configuration.Formatters
                .JsonFormatter.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();*/

            var json = GlobalConfiguration.Configuration.Formatters.JsonFormatter;
            json.SerializerSettings.PreserveReferencesHandling = PreserveReferencesHandling.None;
            json.SerializerSettings.DateTimeZoneHandling = DateTimeZoneHandling.Utc;
            json.SerializerSettings.Re‌​ferenceLoopHandling = ReferenceLoopHandling.Ignore;


            IScheduler scheduler = StdSchedulerFactory.GetDefaultScheduler();
            
            IJobDetail job = JobBuilder.Create<NightAuditJob>()
                .StoreDurably()
                .WithIdentity("nightAudit", "scheduler")
                .Build();

            scheduler.AddJob(job, true);

            ITrigger trigger = TriggerBuilder.Create()
                .WithIdentity("nightAuditTrigger", "scheduler")
                .StartNow()
                .WithSimpleSchedule(x => x.WithMisfireHandlingInstructionIgnoreMisfires()
                    .WithIntervalInMinutes(1)
                    .RepeatForever())
                .ForJob(job)
                .Build();

            IJobDetail paymentJob = JobBuilder.Create<PaymentAuditJob>()
                .StoreDurably()
                .WithIdentity("paymentAudit", "scheduler")
                .Build();

            scheduler.AddJob(paymentJob, true);

            ITrigger paymentTrigger = TriggerBuilder.Create()
                .WithIdentity("paymentAuditTrigger", "scheduler")
                .StartNow()
                .WithSimpleSchedule(x => x.WithMisfireHandlingInstructionIgnoreMisfires()
                    .WithIntervalInMinutes(59)
                    .RepeatForever())
                .ForJob(paymentJob)
                .Build();

            IJobDetail cmBackgroundJob = JobBuilder.Create<ChannelManagerBackgroundJob>()
               .StoreDurably()
               .WithIdentity("cmBackgroundAudit", "scheduler")
               .Build();

            scheduler.AddJob(cmBackgroundJob, true);

            ITrigger cmBackgroundTrigger = TriggerBuilder.Create()
                .WithIdentity("cmBackgroundAuditTrigger", "scheduler")
                .StartNow()
                .WithSimpleSchedule(x => x.WithMisfireHandlingInstructionIgnoreMisfires()
                    .WithIntervalInSeconds(59)
                    .RepeatForever())
                .ForJob(cmBackgroundJob)
                .Build();

            IJobDetail cmAutoAssignRoomJob = JobBuilder.Create<ChannelManagerAutoAssignRoomJob>()
               .StoreDurably()
               .WithIdentity("cmAutoAssignRoomAudit", "scheduler")
               .Build();

            scheduler.AddJob(cmAutoAssignRoomJob, true);

            ITrigger cmAutoAssignRoomTrigger = TriggerBuilder.Create()
                .WithIdentity("cmAutoAssignRoomTrigger", "scheduler")
                .StartNow()
                .WithSimpleSchedule(x => x.WithMisfireHandlingInstructionIgnoreMisfires()
                    .WithIntervalInSeconds(10)
                    .RepeatForever())
                .ForJob(cmAutoAssignRoomJob)
                .Build();

            scheduler.ScheduleJob(cmBackgroundTrigger);
            scheduler.ScheduleJob(paymentTrigger);
            scheduler.ScheduleJob(trigger);
            scheduler.ScheduleJob(cmAutoAssignRoomTrigger);

            scheduler.Start();

        }
       

    }
    public class NightAuditJob: IJob
    {
        public void Execute(IJobExecutionContext context)
        {
            System.Diagnostics.Debug.WriteLine("Hello Night Audit");
            Common.NightAudit.NightAuditScheduler();
        }
    }
    public class PaymentAuditJob : IJob
    {
        public void Execute(IJobExecutionContext context)
        {
            System.Diagnostics.Debug.WriteLine("Hello Payment Audit");
            Common.NightAudit.PaymentAuditScheduler();
        }
    }

    public class ChannelManagerBackgroundJob : IJob
    {
        public void Execute(IJobExecutionContext context)
        {
            Common.ChannelManagerBackgroundJob.ChannelManagerAutoBackgroundJobScheduler();
        }
    }

    public class ChannelManagerAutoAssignRoomJob : IJob
    {
        public async void Execute(IJobExecutionContext context)
        {

            if (WebApiApplication.flagRunProcessAutoAssignRoom==true)
            {
                WebApiApplication.flagRunProcessAutoAssignRoom = false;
                await Common.STAAHReservationsUtility.ChannelManagerAutoAssignRoomJobScheduler();
            }

        }
    }

    public class AutoDeletePOSDraftJob : IJob
    {
        public void Execute(IJobExecutionContext context)
        {
            Common.NightAudit.AutoDeletePOSDraftScheduler();
        }
    }
}
